﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DbGenerator.Factory.events;
using System.Data.SqlClient;
using DbGenerator.Factory.generator;
using DbGenerator.Model.units.table;
using DbGenerator.Model.units.column;
using DbExecuter.DataLayer.commands;
using Unity.Errors.types;
using DbExecuter.DataLayer.executer;

namespace DbGenerator.Test.WinUI
{
    public partial class TEntityForm : Form
    {
        public TEntityForm()
        {
            InitializeComponent();
        }

        private void ShowGenerationForm()
        {
            dbGenerationForm = new TDatabaseGenerationForm();
            dbGenerationForm.DbGenerationUC.ReadCompletedEvent
                += new DReadDatabaseCompleted(DbGenerationUC_ReadCompletedEvent);
            dbGenerationForm.ShowDialog();
        }
        #region private fields
        TDatabaseGenerationForm dbGenerationForm;
        #endregion

        #region handlers
        void DbGenerationUC_ReadCompletedEvent(TFactoryArgs args)
        {
            gbxDatabaseReport.Enabled = true;
            lbxTables.Items.Clear();
            lbxDatabases.Items.Add(args.Database.DatabaseName);
            lbxDatabases.Enabled = true;
            gbxTableSequences.Enabled = true;
        }
        private void lbxDatabases_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbxDatabases.SelectedIndex == 0)
                FillTableList();
        }
        private void lbxTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillColumnsByTableName(lbxTables.SelectedItem.ToString());
            gbxExecuteCommands.Enabled = true;
        }
        private void tsmiReadOrGenerateDatabase_Click(object sender, EventArgs e)
        {
            ShowGenerationForm();
        }
        private void cmbCommandType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbCommandType.SelectedIndex == -1)
                return;
            if (cmbCommandType.SelectedItem.ToString() == "Select All")
            {
                SwitchDynamicControls("Select");
                TExecuter exec = new TExecuter(GetCurrentTable()
                    , dbGenerationForm.DbGenerationUC.GeneArgs.MasterConnection);
                dgvSelectResults.DataSource = null;
                dgvSelectResults.DataSource = exec.SelectAll();
            }
        }

        private void SwitchDynamicControls(string commandType)
        {
            if (commandType == "Select")
            {
                dgvSelectResults.Visible = true;
            }
        }


        private void btnInsertSafe_Click(object sender, EventArgs e)
        {
            lbxInsertSafe.DataSource = 
                dbGenerationForm.DbGenerationUC.GeneArgs.Database.GetInsertSafeTableSequence();
        }

        private void btnDeleteSafe_Click(object sender, EventArgs e)
        {
            lbxDeleteSafe.DataSource =
                 dbGenerationForm.DbGenerationUC.GeneArgs.Database.GetDeleteSafeTableSequence();
        }
        #endregion

        #region private methods
        private void FillTableList()
        {
            lbxTables.Items.Clear();
            foreach (TTable t in dbGenerationForm.DbGenerationUC.GeneArgs.Database.Tables)
            {
                lbxTables.Items.Add(t.TableSign.TableName);
            }
            lbxTables.Enabled = true;
        }
        private void FillColumnsByTableName(string tableName)
        {
            clbSelectColumns.Items.Clear();
            TTable t = GetCurrentTable();
            foreach (TColumn c in t.Columns)
            {
                clbSelectColumns.Items.Add(c.ColumnInfo.ColumnName);
            }
            clbSelectColumns.Enabled = true;
        }
        private TColumn[] GetSelectedColumns()
        {
            TTable currentTable = GetCurrentTable();
            int checkedColumnCount = clbSelectColumns.CheckedItems.Count;
            string[] columnNames = new string[checkedColumnCount];
            for (int i = 0; i < checkedColumnCount; i++)
            {
                columnNames[i] = clbSelectColumns.CheckedItems[i].ToString();
            }

            TColumn[] columns = new TColumn[checkedColumnCount];
            for (int i = 0; i < checkedColumnCount; i++)
            {
                columns[i] = GetColumnByName(columnNames[i], currentTable);
            }

            return columns;
        }
        private TColumn GetColumnByName(string columnName, TTable currentTable)
        {
            int columnCount = currentTable.Columns.Count;
            for (int i = 0; i < columnCount; i++)
            {
                if (currentTable.Columns[i].ColumnInfo.ColumnName == columnName)
                    return currentTable.Columns[i];
            }
            return null;
        }
        private TTable GetCurrentTable()
        {
            TTable t = null;
            string tableName = lbxTables.SelectedItem.ToString();
            foreach (TTable tbl in dbGenerationForm.DbGenerationUC.GeneArgs.Database.Tables)
            {
                if (tbl.TableSign.TableName == tableName)
                    t = tbl;
            }
            return t;
        }
        #endregion


    }
}
